package com.cy.linklist;

import java.util.Stack;

/**
 * @Author: chenyun
 * @Date: 2021/09/15/16:00
 * @Description:
 */

//剑指 Offer 06. 从尾到头打印链表
class Demo06 {
    public static int[] reversePrint(ListNode head) {
        Stack<Object> stack = new Stack<>();
        ListNode listNode = head;
        while (listNode != null){
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        int size = stack.size();
        int[] p = new int[size];
        for (int i = 0; i < size; i++) {
            int pop = (int) stack.pop();
            p[i] = pop;
        }
        return p;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = null;
        ListNode head = listNode1;
        System.out.println("链表显示数据顺序");
        while (head != null){
            System.out.print(head.val);
            head = head.next;
        }
        int[] arr = reversePrint(listNode1);
        System.out.println();
        System.out.println("链表从尾到头的数据");
        for (int i : arr) {
            System.out.print(i);
        }

    }

}
